package com.qf.huigou.Service.impl;

import com.qf.huigou.Service.PhoneService;
import com.qf.huigou.dao.PhoneDao;
import com.qf.huigou.dto.Assess;
import com.qf.huigou.entity.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PhoneServiceImpl implements PhoneService {

    @Resource
    PhoneDao phoneDao;

    @Override
    public List<String> selectBrandNames(int status) {
        return phoneDao.selectBrandNames(status);
    }


    /**
     * 分页查询
     * @param pageNo
     * @param phone_name
     * @param brand_name
     * @return
     */
    public Page<Phone> selectByPage(int pageNo,String phone_name,String brand_name) {
        //参数条件包装类，用于做动态sql,包括brand_name,phone_name,start,length
        Options options = new Options();
        options.setPhone_name(phone_name);
        options.setBrand_name(brand_name);
        //总数
        int counts = phoneDao.selectCounts(options);
        //页数
        int pageCounts = (counts % Page.PAGE_ROWS==0)?(counts/Page.PAGE_ROWS):(counts/Page.PAGE_ROWS+1);
        options.setStart((pageNo - 1) * Page.PAGE_ROWS);
        options.setLength(Page.PAGE_ROWS);
        //手机信息集合
        List<Phone> data = phoneDao.selectByPage(options);
        //页面对象，包含页面的所有信息
        Page<Phone> page = new Page<Phone>();
        page.setCurrentPage(pageNo);
        page.setPageCounts(pageCounts);
        page.setData(data);
        page.setTotalCounts(counts);
        return page;
    }

    @Override
    public PageInfo selectAssessInfo(int id) {
        Phone phone = phoneDao.selectPhoneById(id);
        List<Assess> data = phoneDao.selectAssessInfo(id);
        PageInfo page = new PageInfo();
        page.setData(data);
        page.setPhone(phone);
        return page;
    }

}
